package com.test.demo.annotation.proxy.http.factory;

import com.test.demo.annotation.proxy.http.handler.MyFeginClientSupport;
import com.test.demo.annotation.proxy.http.handler.MyInvocationHandler;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Proxy;

/**
 * @ClassName AbstractMyFeginClientFactoryBean
 * @Description
 * @Author laigl
 * @Date 2020/2/22 22:46
 **/
public class MyAbstractFactoryBean<T> implements FactoryBean<T> {

    private Class<?> objectType;

    @Autowired
    private MyFeginClientSupport myFeginClientSupport;

    @Override
    public T getObject() {
        //通过jdk代理生成代理对象
        return (T) Proxy.newProxyInstance(getClass().getClassLoader(),
                new Class[]{objectType},
                new MyInvocationHandler(myFeginClientSupport));
    }

    @Override
    public Class getObjectType() {
        return objectType;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    //用注入的方式给 生成bean的类型 设值
    public void setObjectType(Class<?> objectType) {
        this.objectType = objectType;
    }

}
